package com.example.anpuser.controller.crud;

import com.example.anpapi.entity.po.usermodel.Author.Author;
import com.example.anpapi.entity.vo.R;
import com.example.anpuser.service.interfaces.AuthorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/author")
public class AuthorController {

    @Autowired
    private AuthorService authorService;

    /**
     * 作者信息添加
     * @param author
     * @return
     */
    @PostMapping("/save")
    public R<Author> save(@RequestBody Author author){
        log.info("作者信息添加:{}", author);
        authorService.save(author);
        return R.success(author,"作者信息添加成功");
    }

    /**
     * 根据作者id删除作者信息
     * @param author
     * @return
     */
    @DeleteMapping("/remove")
    public R<String> remove(@RequestBody Author author){
        log.info("作者信息删除:{}", author);
        if(authorService.getById(author.getId()) != null)
        {
            authorService.removeById(author.getId());
            return R.success(null,"作者信息删除成功");
        }
        return R.error("未找到该作者信息");
    }

    /**
     * 根据作者id查找作者信息
     * @param id
     * @return
     */
    @GetMapping("/get")
    public R<Author> get(@RequestParam(value = "id") Long id) {
        log.info("查询作者的id为{}", id);
        Author author = authorService.getById(id);
        return R.success(author, "查询作者信息成功");
    }

    /**
     * 根据用户id查找作者信息
     * @param userId
     * @return
     */
    @GetMapping("/getByUserId")
    public R<Author> getByUserId(@RequestParam(value = "userId") Long userId) {
        log.info("查询作者的userId为{}", userId);
        Author author = authorService.lambdaQuery()
                .eq(Author::getUserId, userId)
                .one();
        return R.success(author, "查询作者信息成功");
    }

    /**
     * 修改作者信息
     * @param author
     * @return
     */
    @PostMapping("/update")
    public R<Author> update(@RequestBody Author author){
        log.info("修改的作者信息为{}", author);
        authorService.updateById(author);
        return R.success(author,"修改作者信息成功");
    }

}


